package cn.itmantzy.org.service.impl;

import cn.itmantzy.basic.service.impl.BaseServiceImpl;
import cn.itmantzy.basic.util.PageList;
import cn.itmantzy.org.domain.Department;
import cn.itmantzy.org.mapper.DepartmentMapper;
import cn.itmantzy.org.query.DepartmentQuery;
import cn.itmantzy.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        // 查询出该对象的父对象
        Department parent = department.getDepartment();
        // 1.如果为顶级部门，dirPath直接就是 / 加上自己的id
        String dirPath;
        if (parent == null){
            dirPath = "/" + department.getId();
        }else {
            Department parent1 = departmentMapper.loadById(parent.getId());
            dirPath = parent1.getDirPath() + "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    public List<Department> findOneAndTwoLevelTree() {
        return departmentMapper.loadOneAndTwoLevelTree();
    }
}
